package com.example.conusmer.interrupt

import com.example.conusmer.common.*
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Pointcut
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Component

@Aspect
@Component
class ResponseAspect {

    @Pointcut("execution(* com.example.conusmer.controller.*.*(..))")
    fun controllerMethods() {}

    @Around("controllerMethods()")
    fun aroundControllerMethods(joinPoint: ProceedingJoinPoint): Any {
        try {
            val result = joinPoint.proceed()
            if (result is ResponseEntity<*>) {
                val data = result.body
                return   ResponseEntity.ok(successReturn(data))
            }
            return result
        } catch (e: Throwable) {
            throw e
        }
    }

}
